home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / spoc88 / dbdemo / demo.c next >
Text File  |  1988-07-01  |  4KB  |  99 lines

  1. /*
  2.  * Turbo C 2.0 program for use in a sample Turbo Debugger
  3.  * debugging session. The bug: the Text array in the
  4.  * TextBlock structure does not include space for the
  5.  * terminating zero byte. The solution: dimension the
  6.  * Text array to (BUFFER_LENGTH + 1) characters in length.
  7.  *
  8.  * By Michael Abrash 6/18/88
  9.  */
  10. #include <stdio.h>
  11. #include <alloc.h>
  12.  
  13. /* Number of characters buffered per text block. */
  14. #define BUFFER_LENGTH   20
  15.  
  16. /* Structure we'll use to store text in. These structures
  17.    are combined into a singly-linked list, with one
  18.    structure per allocated memory buffer. */
  19. struct TextBlock {
  20.    char Text[BUFFER_LENGTH];        /* text buffer */
  21.    struct TextBlock *NextTextBlock; /* pointer to next
  22.                                        text block */
  23. };
  24.  
  25. main()
  26. {
  27.    int c;         /* temporary storage for a character */
  28.    int Done = 0;  /* set to 1 when all text is buffered */
  29.    int TextCount; /* location in the current text buffer */
  30.    struct TextBlock *FirstTextBlock;
  31.                   /* Points to the text block that
  32.                      starts the linked chain. */
  33.    struct TextBlock *CurrentTextBlock;
  34.                   /* points to the current text block */
  35.    struct TextBlock *NewTextBlock;
  36.                   /* points to the next text block */
  37.  
  38.    /* Get the initial text block */
  39.    if ( !(FirstTextBlock = CurrentTextBlock =
  40.       malloc(sizeof(struct TextBlock))) ) {
  41.       /* We couldn't get any memory */
  42.       printf("Out of memory\n");
  43.       exit(1);
  44.    }
  45.  
  46.    /* Buffer the text the user types, allocating memory as
  47.       it's needed */
  48.    TextCount = 0;
  49.    while ( !Done ) {
  50.       /* Get the next character */
  51.       c = getchar();
  52.       if ( c == EOF ) {
  53.          /* It's the end of the file, so we're done */
  54.          /* Put a zero at the end of the current buffer,
  55.             making it a string */
  56.          CurrentTextBlock->Text[TextCount] = 0;
  57.          /* Mark that this is the last text block in the
  58.             linked list */
  59.          CurrentTextBlock->NextTextBlock = 0;
  60.          /* We've gotten all the text */
  61.          Done = 1;
  62.       } else {
  63.          /* Buffer the character */
  64.          CurrentTextBlock->Text[TextCount++] = toupper(c);
  65.          if ( TextCount >= BUFFER_LENGTH ) {
  66.             /* This buffer's full, so allocate another
  67.                text block */
  68.             if ( !(NewTextBlock =
  69.                CurrentTextBlock->NextTextBlock =
  70.                malloc(sizeof(struct TextBlock))) ) {
  71.                /* We couldn't get any more memory */
  72.                printf("Out of memory\n");
  73.                exit(1);
  74.             }
  75.             /* Put a zero at the end of the current buffer,
  76.                making it a string */
  77.             CurrentTextBlock->Text[TextCount] = 0;
  78.             /* Start buffering at the beginning of this
  79.                text block's text buffer */
  80.             TextCount = 0;
  81.             /* Make the newly allocated text block the
  82.                current text block */
  83.             CurrentTextBlock = NewTextBlock;
  84.          }
  85.       }
  86.    }
  87.  
  88.    /* Print out the uppercase result, starting with the
  89.       text stored in the first text block and continuing
  90.       until the last text block (the text block with a
  91.       null link) has been displayed */
  92.    CurrentTextBlock = FirstTextBlock;
  93.    do {
  94.       printf("%s", CurrentTextBlock->Text);
  95.       CurrentTextBlock = CurrentTextBlock->NextTextBlock;
  96.    } while ( CurrentTextBlock );
  97. }
  98.  
  99.